package activity

import (
	"context"

	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/promotion/rpc/promotionclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateActivityCouponLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateActivityCouponLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateActivityCouponLogic {
	return &CreateActivityCouponLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateActivityCouponLogic) CreateActivityCoupon(
	req *types.CreateActivityCouponReq,
) (resp *types.BaseMsgResp, err error) {
	if req.ActivityId != nil && *req.ActivityId > 0 && len(req.Data) > 0 {
		var coupons []*promotionclient.ActivityCouponInfo
		for _, v := range req.Data {
			coupons = append(coupons, &promotionclient.ActivityCouponInfo{
				ActivityId:  req.ActivityId,
				CouponId:    v.CouponId,
				PerLimit:    v.PerLimit,
			})
		}

		res, err := l.svcCtx.PromotionRpc.BackendCreateActivityCoupon(l.ctx, &promotionclient.ActivityRelationshipInfo{
			ActivityId: *req.ActivityId,
			Coupons:    coupons,
		})
		if err != nil {
			return nil, err
		}

		return &types.BaseMsgResp{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, res.Msg),
		}, nil
	}

	return
}
